package com.hoshiicloud.market.service.impl.market;

import com.hoshiicloud.goods.entity.spu.SpuSalesCategory;
import com.hoshiicloud.market.mapper.market.SpuSalesCategoryMapper;
import com.hoshiicloud.market.service.market.SpuSalesCategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hoshiicloud.common.aop.Logable;
import com.hoshiicloud.common.exception.CommonError;
import com.hoshiicloud.common.exception.ServiceException;
import com.hoshiicloud.market.feign.IdGeneratorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDateTime;
import java.util.List;
/**
 * <p>
 * 营销类目 服务实现类
 * </p>
 *
 * @author zhangshuhu
 * @since 2019-06-04
 */
@Service
public class SpuSalesCategoryServiceImpl extends ServiceImpl<SpuSalesCategoryMapper, SpuSalesCategory> implements SpuSalesCategoryService {

    @Autowired
    private IdGeneratorService idGeneratorService;

    /**
    * @param id
    * @return SpuSalesCategory
    * @description: 根据id查询营销类目
    */
    @Cacheable(cacheNames = "SpuSalesCategory", key = "#root.targetClass.simpleName+':'+#root.methodName+':'+#id")
    @Logable(authName = "商品管理-营销类目-查询营销类目 ")
    public SpuSalesCategory getById(Long id) {
        return baseMapper.selectById(id);
    }


    /**
    * @param entity
    * @return boolean
    * @description: 新增营销类目
    */
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "SpuSalesCategory", allEntries = true)
    @Logable(authName = "商品管理-营销类目-新增营销类目")
    public boolean save(SpuSalesCategory entity) {
        String id = idGeneratorService.queryGeneratorId();
        if (id == null || id == "") {
            throw new ServiceException(CommonError.error("新增失败"));
        }
        entity.setId(Long.parseLong(id));
        entity.setCreatedTime(LocalDateTime.now());
        int row = baseMapper.insert(entity);
        if (row <= 0) {
            throw new ServiceException(CommonError.error("新增失败"));
        }
        return true;
    }

    /**
    * @param entity
    * @return boolean
    * @description: 修改营销类目
    */
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "SpuSalesCategory", allEntries = true)
    @Logable(authName = "商品管理-营销类目管理-修改营销类目")
    public boolean updateById(SpuSalesCategory entity) {
        entity.setUpdatedTime(LocalDateTime.now());
        int row = baseMapper.updateById(entity);
        if (row <= 0) {
            throw new ServiceException(CommonError.error("修改失败"));
        }
        return true;
    }

    /**
    * @param queryWrapper
    * @return IPage
    * @description: 分页查询营销类目
    */
    @Logable(authName = "商品管理-营销类目管理-分页查询营销类目")
    @Cacheable(cacheNames = "SpuSalesCategory", key = "T(com.hoshiicloud.common.utils.DigestUtils).md5('Page(current=' + #page.getCurrent() + ', size=' + #page.getSize() + '), Wrapper(SqlSelect=' + #queryWrapper.getSqlSelect() + ', SqlSegment=' + #queryWrapper.getSqlSegment() + ',queryWrapper=' + #queryWrapper.toString() + ')')")
    public IPage<SpuSalesCategory> page(IPage<SpuSalesCategory> page, Wrapper<SpuSalesCategory> queryWrapper) {
        return baseMapper.selectPage(page, queryWrapper);
    }

    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "SpuSalesCategory", allEntries = true)
    @Logable(authName = "商品管理-营销类目管理-删除营销类目")
    public boolean deleteById(SpuSalesCategory entity) {
        entity.setUpdatedTime(LocalDateTime.now());
        entity.setDeleted(1);
        int row = baseMapper.updateById(entity);
        if (row <= 0) {
            throw new ServiceException(CommonError.error("删除失败"));
        }
        return true;
    }

    @Cacheable(cacheNames = "SpuSalesCategory", key = "#root.targetClass.simpleName+':'+#root.methodName+':'+#key")
    @Logable(authName = "商品管理-营销类目管理-根据id集合查询营销类目")
    public List<SpuSalesCategory> getByIds(List<Long> ids,String key) {
        return baseMapper.selectBatchIds(ids);
    }
}
